﻿/*************************************************************************
 * 
 * Filename :config
 * 
 * steven hu   2010/3/8 15:58:38
 *  
 * http://www.cnblogs.com/huxj
 *   
 * 
 * Change History:
 * 
 * 
**************************************************************************/


using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;

namespace RicciWebsite
{

    public class configHelper
    {
        private static string configpath = HttpContext.Current.Server.MapPath("~/config/websiteconfig.xml");
        private static object lockObj = new object();

        /// <summary>
        /// 返回 配置
        /// </summary>
        /// <returns></returns>
        public static config Get()
        {
            config configInfo = Common.DefaultCache.GetCache("website_config_xml") as config;
            if (null == configInfo)
            {
                lock (lockObj)
                {
                    if (null == configInfo)
                    {
                        configInfo = LoadXml();

                        Common.DefaultCache.AddCacheFilesDependency("website_config_xml", configInfo, configpath);
                    }
                }
            }
            return configInfo;
        }

        
        /// <summary>
        /// 设置配置文件
        /// </summary>
        /// <param name="configInfo"></param>
        public static void Set(config configInfo)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(configpath);

            XmlCDataSection cdata = doc.CreateCDataSection(configInfo.Copyright);
            XmlNode node = doc.GetElementsByTagName("copyright").Item(0);
            node.RemoveAll();
            node.AppendChild(cdata);


            cdata = doc.CreateCDataSection(configInfo.Email);
            node = doc.GetElementsByTagName("email").Item(0);
            node.RemoveAll();
            node.AppendChild(cdata);

            cdata = doc.CreateCDataSection(configInfo.Fax);
            node = doc.GetElementsByTagName("fax").Item(0);
            node.RemoveAll();
            node.AppendChild(cdata);


            cdata = doc.CreateCDataSection(configInfo.Phone);
            node = doc.GetElementsByTagName("phone").Item(0);
            node.RemoveAll();
            node.AppendChild(cdata);

            cdata = doc.CreateCDataSection(configInfo.Websitename);
            node = doc.GetElementsByTagName("websitename").Item(0);
            node.RemoveAll();
            node.AppendChild(cdata);

            cdata = doc.CreateCDataSection(configInfo.Msnurl);
            node = doc.GetElementsByTagName("msn").Item(0);
            node.RemoveAll();
            node.AppendChild(cdata);
            XmlAttribute atti = doc.CreateAttribute("name");
            atti.Value = configInfo.Msn;
            node.Attributes.Append(atti);

            cdata = doc.CreateCDataSection(configInfo.Logo);
            node = doc.GetElementsByTagName("logo").Item(0);
            node.RemoveAll();
            node.AppendChild(cdata);

            doc.Save(configpath);

        }


        /// <summary>
        /// 读取xml配置
        /// </summary>
        /// <returns></returns>
        public static config LoadXml()
        {
            config configInfo = new config();

            XmlDocument doc = new XmlDocument();
            doc.Load(configpath);
            configInfo.Copyright = doc.GetElementsByTagName("copyright").Item(0).InnerText;
            configInfo.Email = doc.GetElementsByTagName("email").Item(0).InnerText;
            configInfo.Fax = doc.GetElementsByTagName("fax").Item(0).InnerText;
            configInfo.Msn = doc.GetElementsByTagName("msn").Item(0).Attributes["name"].Value;
            configInfo.Msnurl = doc.GetElementsByTagName("msn").Item(0).InnerText;
            configInfo.Phone = doc.GetElementsByTagName("phone").Item(0).InnerText;
            configInfo.Websitename = doc.GetElementsByTagName("websitename").Item(0).InnerText;
            configInfo.Logo = doc.GetElementsByTagName("logo").Item(0).InnerText;
            return configInfo;
        }
    }


    /// <summary>
    /// 配置信息
    /// </summary>
    public class config
    {
        private string copyright;
        /// <summary>
        ///  版权
        /// </summary>
        public string Copyright
        {
            get { return copyright; }
            set { copyright = value; }
        }

        private string websitename;
        /// <summary>
        /// 网站名称
        /// </summary>
        public string Websitename
        {
            get { return websitename; }
            set { websitename = value; }
        }

        private string msn;
        /// <summary>
        /// MSN
        /// </summary>
        public string Msn
        {
            get { return msn; }
            set { msn = value; }
        }


        private string msnurl;
        /// <summary>
        /// Msn的weburl
        /// </summary>
        public string Msnurl
        {
            get { return msnurl; }
            set { msnurl = value; }
        }

        private string email;
        /// <summary>
        /// Email
        /// </summary>
        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        private string phone;
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        private string fax;
        /// <summary>
        /// 传真
        /// </summary>
        public string Fax
        {
            get { return fax; }
            set { fax = value; }
        }

        private string logo;

        /// <summary>
        /// LOGO路径
        /// </summary>
        public string Logo
        {
            get { return logo; }
            set { logo = value; }
        }
    }
}
